<?php
namespace Admin\Controller;
use Admin\Model\AreaModel;
use Admin\Model\PriceModel;
use \Common\Model\CommonModel;
use Think\Controller;

class PriceController extends BaseController{
    //主页
    public function index(){
        $priceModel=M('price');
        if(!empty($_GET['area_id'])){
            unset($_SESSION['like']['price']);
            $_SESSION['like']['price']=$_GET['area_id'];
            $condition1['name']=array('like',"%".$_GET['area_id']."%");
            $areas=M('area')->where($condition1)->select();
            $area_ids=array();
            foreach($areas as $key=>$value){
                $area_ids[]=$value['id'];
            }
            if(!empty($area_ids)){
                $condition['area_id']=array('in',$area_ids);
            }else{
                $this->display();
                exit;
            }
        }
        $condition['area_id'] = array('like',"%".$_GET['area_id']."%");
        if($_GET['start']&&$_GET['end']){
            $condition['create']= array('between',array(strtotime($_GET['start']),strtotime($_GET['end'])));
        }
        $count = $priceModel->where($condition)->count();
        $page= new \Think\Page($count,1);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$priceModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();

        $list=CommonModel::status($list);
        $areaModel=new AreaModel();
        $list=$areaModel->getArea($list);

        //var_dump($list);exit;
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }

    //修改页面
    public function update(){
        $priceModel=D('price');
        //$priceModel=new PriceModel();
        $da['id']=$_GET['id'];
       // $list=$this->finded($da);
        //找到原数据
        $list=$priceModel->finded($da);
        $areaModel=new AreaModel();
        $areas=$areaModel->getAll();
        $this->assign('areas',$areas);
        $this->assign('list',$list);
        $this->display();
    }
    //执行修改
    public function doUpdate(){
        $priceModel=D('price');
        $data['mobile']=$_POST['mobile'];
        $data['area_id']=$_POST['area_id'];
        $data['plot']=$_POST['plot'];
        $data['price']=$_POST['price'];
        $data['city']=$_POST['city'];
        $data['sex']=$_POST['sex'];
        $data['status']=$_POST['status'];
        $id=$_POST['id'];
        $where='id='.$id;
        $list=$priceModel->finded($where);
       /* echo(json_encode($list));
        exit;*/
        $data['update']=time();
        //过滤重复内容
        $data=CommonModel::filter($data,$list);
        /*echo(json_encode($data));
        exit;*/
        $res=$priceModel->update($where,$data);
        if($res){
            $this->ajaxReturn(1);
        }else{
            $this->ajaxReturn(2);
        }
    }
    //删除
    public function del(){
        $priceModel=D('price');
        $id=$_GET['id'];
        $res= $priceModel->deleted('id='.$id);
        if($res){
           $this->success('删除成功');
        }else{
           $this->error('删除失败');
        }
    }

}